package com.irheima.edu.manager.service;

import com.irheima.edu.manager.dao.StudentDao;
import com.irheima.edu.manager.domain.Student;

public class StudentService {

    // 创建StudentDao (库管)
    //将数据访问对象注入到当前类中
    private StudentDao studentDao = new StudentDao();

    public boolean addStudent(Student stu) {


        // 2. 将学生对象, 传递给StudentDao 库管中的addStudent方法
        // 3. 将返回的boolean类型结果, 返还给StudentController
        return studentDao.addStudent(stu);
        //studentService

    }

    public boolean isExists(String id) {
        //判断-- 调用dao所有学生对象；
        Student[] students = studentDao.findAllStudent();
        boolean exists = false;
        for (int i = 0; i < students.length; i++) {
            Student stu = students[i];
            if (stu != null && stu.getId().equals(id)) {
                exists = true;
                break;
            }

        }

        return exists;
    }
    //获取所有学生数据
    public Student[] findAllStudent() {
        Student[] students =studentDao.findAllStudent();

        //判断
        for (int i = 0; i < students.length; i++) {
            if(students[i] != null ){
                return students;
            }
        }
        return null;
    }

    public void deleteStudentById(String delId) {
        studentDao.deleteStudentById(delId);
    }
    public  int getIndexById(String id){
        Student[] students = studentDao.findAllStudent();
        for (int i = 0; i < students.length; i++) {
            if(students[i]!=null&&students[i].getId().equals(id)){
                return i;
            }
        }
        return -1;
    }

    public void updateStudent(String updateId, Student newStu) {
        studentDao.updateStudent(updateId,newStu);

    }
}
